package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.Result;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.JobLevel;
import com.entity.PersonnelTransfer;
import com.entity.Position;
import com.service.PersonnelTransferService;
import com.service.sys.DepartmentService;
import com.service.sys.UserVoService;
import com.vo.PersonnelTransferVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/personnelTransfer")
public class PersonnelTransferController {

    @Resource
    private PersonnelTransferService personnelTransferService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserVoService userVoService;

    @ApiOperation("修改人员调用")
    @PostMapping("/update")
    @OperationLog(operationType = OperationType.UPDATE, describe = "修改人员调用", entity = PersonnelTransfer.class, primaryKey = "id")
    public Result update(@RequestBody PersonnelTransfer personnelTransfer) {
        return Result.success(personnelTransferService.updateByPrimary(personnelTransfer));
    }

    @ApiOperation("通过id获取人员调动信息")
    @GetMapping("/info/{id}")
    public Result info(@PathVariable String id) {
        return  Result.success(personnelTransferService.getById(id));
    }

    @ApiOperation("查询人员调用")
    @PostMapping(value = "/select")
    public Result selectDepartment(@RequestBody PersonnelTransfer personnelTransfer) {
        QueryWrapper<PersonnelTransfer> wrapper = new QueryWrapper<>();
        wrapper.eq(
                !StringUtils.isEmpty(personnelTransfer.getStatu()),
                "statu",
                personnelTransfer.getStatu()
        );
        List<PersonnelTransfer> personnelTransferList = personnelTransferService.list(wrapper);
        List<PersonnelTransferVo> voList = new ArrayList<>();
        for (PersonnelTransfer transfer : personnelTransferList) {
            PersonnelTransferVo personnelTransferVo = new PersonnelTransferVo();
            personnelTransferVo.setPersonnelTransfer(transfer);
            personnelTransferVo.setBeforeDepartmentVo(departmentService.getById(transfer.getBeforeDepartment()));
            personnelTransferVo.setAfterDepartmentVo(departmentService.getById(transfer.getAfterDepartment()));
            personnelTransferVo.setUserVo(userVoService.getById(transfer.getUserId()));
            voList.add(personnelTransferVo);
        }
        return Result.success(voList);
    }

}
